/**
 * 报关单表头表单的弹窗mixins
 * 其他包装
 * 备注
 * 其他事项弹窗
 * 标记唛码
 * 业务事项弹窗
 * 特殊业务标识
 * 检验检疫申报要素
 * 企业资质
 * 使用人
 */

import packingModal from '../../../packingModal'
import otherMatterModal from '../../../otherMatterModal'
import remarkModal from '../../../remarkModal'
import markerCodeModal from '../../../markerCodeModal'
import busMatterModal from '../../../busMatterModal'
import specialIdenModal from '../../../specialIdenModal'
import CIQdeclareEleModal from '../../../CIQdeclareEleModal'
import usePeopModal from '../../../usePeopModal'
import qualificationsModal from '../../../qualificationsModal'

export default {
    components: {
        packingModal,
        otherMatterModal,
        remarkModal,
        busMatterModal,
        markerCodeModal,
        specialIdenModal,
        CIQdeclareEleModal,
        qualificationsModal,
        usePeopModal
    },
    data () {
        return {
            modalParams: {
                packingShow: false,
                packingParams: {},

                remarkShow: false,
                remarkParams: {},

                otherMatterShow: false,
                otherMatterParams: {},

                markerCodeShow: false,
                markerCodeParams: {},

                busMatterShow: false,
                busMatterParams: {},

                specialIdenShow: false,
                specialIdenParams: {},

                CIQdeclareEleShow: false,
                CIQdeclareEleParams: {},

                usePeopModalShow: false,
                usePeopModalParams: {},

                qualificationsShow: false,
                qualificationsParams: {}
            }
        }
    },
    methods: {
        // 打开其他包装弹窗
        openOtherPacking () {
            this.modalParams.packingParams = Object.freeze({
                editORshow: this.diabled,
                tempArr: this.form.otherWrap
                    ? this.form.otherWrap.split(',')
                    : []
            })
            this.modalParams.packingShow = true
        },
        // 设置其他包装
        setOtherWrapData (res) {
            this.form.otherWrap = res
            this.focusElement('grossWet')
        },

        // 打开备注
        openRemark () {
            this.modalParams.remarkParams = {
                notes: this.form.notes,
                editORshow: this.diabled
            }
            this.modalParams.remarkShow = true
        },
        // 保存备注
        saveRemark (notes) {
            this.form.notes = notes
        },

        // 打开其他事项
        openOtherMatter () {
            let obj = {
                specialRelaConfirmed: this.form.specialRelaConfirmed,
                pricesConfirmed: this.form.pricesConfirmed,
                paymentConfirmed: this.form.paymentConfirmed,
                type: this.form.businessType,
                editORshow: this.diabled
            }
            this.modalParams.otherMatterParams = Object.freeze(obj)
            this.modalParams.otherMatterShow = true
        },
        // 保存其他事项
        saveOtherMatter (obj) {
            for (let i in obj) {
                this.form[i] = obj[i]
            }
            this.focusElement(this.isImport ? 'billType' : 'markNo')
        },

        // 打开标记唛码弹窗
        openMarkerCode () {
            this.modalParams.markerCodeParams = Object.freeze({
                editORshow: this.diabled,
                markNo: this.form.markNo
            })
            this.modalParams.markerCodeShow = true
        },
        // 保存标记唛码
        saveMarkerCode (markNo) {
            this.form.markNo = markNo
        },
        // 打开业务事项
        openBusMatter () {
            let obj = {
                paperlessTax: this.form.paperlessTax,
                voluntaryTaxation: this.form.voluntaryTaxation,
                sinceNewspaperPay: this.form.sinceNewspaperPay,
                chkSurety: this.form.chkSurety,
                waterTransfer: this.form.waterTransfer,
                editORshow: this.diabled
            }
            this.modalParams.busMatterParams = Object.freeze(obj)
            this.modalParams.busMatterShow = true
        },
        // 保存业务事项
        saveBusMatter (obj) {
            for (let i in obj) {
                this.form[i] = obj[i]
            }
        },

        // 打开特殊业务标识弹窗
        openSpecialIden () {
            this.modalParams.specialIdenParams = Object.freeze({
                tempList: this.form.specDeclFlagName
                    ? this.form.specDeclFlagName.split(',')
                    : [],
                editORshow: this.diabled
            })
            this.modalParams.specialIdenShow = true
        },
        // 保存特殊业务标识
        saveSpecialIden (res) {
            this.form.specDeclFlag = res.codeString
            this.form.specDeclFlagName = res.labelString
        },

        // 打开检验检疫申报要素弹窗
        openDeclareEle () {
            this.modalParams.CIQdeclareEleParams = Object.freeze({
                tempArr: this.form.requestCert
                    ? this.form.requestCert.split(',')
                    : [],
                editORshow: this.diabled
            })
            this.modalParams.CIQdeclareEleShow = true
        },
        // 保存检验检疫申报要素
        saveCIQdeclareEle (res) {
            this.form.requestCert = res.codes
            this.form.appCertName = res.names
        },

        // 打开使用人
        openUsePeop () {
            this.modalParams.usePeopModalParams = Object.freeze({
                editORshow: this.diabled,
                CustomsDecUserList: this.form.customsDecUserList || []
            })
            this.modalParams.usePeopModalShow = true
        },
        // 保存使用人
        saveUsePeop (arr) {
            this.form.customsDecUserList = Object.freeze(arr.slice())
        },

        // 打开企业资质弹窗
        openPriseQua () {
            let declaratioMaterialCode = this.form.declaratioMaterialCode
            let isAgree = !!(declaratioMaterialCode && declaratioMaterialCode === '101040')
            let originList = this.form.customsCopLimitList.slice()
            this.modalParams.qualificationsParams = Object.freeze({
                originList,
                isAgree,
                editORshow: this.diabled,
                type: this.isImport ? 'JK' : 'CK'
            })
            this.modalParams.qualificationsShow = true
        },

        // 保存企业资质
        savePriseQua (res) {
            let qualificationsList = res.qualificationsList
            this.form.declaratioMaterialCode = res.isAgree ? '101040' : ''
            if (qualificationsList.length > 0) {
                this.currentEnterpriseQua = 0
                this.form.entQualifTypeCode = qualificationsList[0].entQualifTypeCode
                this.form.entQualifTypeName = qualificationsList[0].entQualifTypeName
            } else {
                this.currentEnterpriseQua = ''
                this.form.entQualifTypeCode = ''
                this.form.entQualifTypeName = ''
            }
            this.form.customsCopLimitList = Object.freeze(qualificationsList.slice())
        },
        // 上/下一个企业资质
        toggleEnterpriseQua (type) {
            const customsCopLimitList = this.form.customsCopLimitList
            let len = customsCopLimitList.length
            if (len === 0) return
            let index = this.currentEnterpriseQua || 0
            if (type === 'prev') {
                if (index > 0) {
                    index--
                }
            } else if (type === 'next') {
                if (index < len - 1) {
                    index++
                }
            }
            this.currentEnterpriseQua = index
            this.form.entQualifTypeCode = customsCopLimitList[index].entQualifTypeCode
            this.form.entQualifTypeName = customsCopLimitList[index].entQualifTypeName
        }
    }
}
